﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[AddComponentMenu("Tween/Tween Rect Position")]
public class TweenRectPosition : UITweener
{
    public Vector2 from;
    public Vector2 to;

    //[HideInInspector]
    //public bool worldSpace = true;

    RectTransform mTrans;

    public RectTransform cachedTransform {
        get {
            if (mTrans == null)
            {
                mTrans = this.gameObject.GetComponent<RectTransform>();
            }
            return mTrans;
        }
    }

    /// <summary>
    /// Tween's current value.
    /// </summary>

    public Vector2 value
    {
        get
        {
            return cachedTransform.anchoredPosition;
        }
        set
        {
            cachedTransform.anchoredPosition = value;
        }
    }

    /// <summary>
    /// Tween the value.
    /// </summary>

    protected override void OnUpdate(float factor, bool isFinished) { value = from * (1f - factor) + to * factor;  }

    /// <summary>
    /// Start the tweening operation.
    /// </summary>

    ///// <summary>
    ///// Start the tweening operation.
    ///// </summary>

    //static public TweenPosition Begin (GameObject go, float duration, Vector3 pos)
    //{
    //	TweenPosition comp = UITweener.Begin<TweenPosition>(go, duration);
    //	//comp.worldSpace = worldSpace;
    //	comp.from = comp.value;
    //	comp.to = pos;

    //	if (duration <= 0f)
    //	{
    //		comp.Sample(1f, true);
    //		comp.enabled = false;
    //	}
    //	return comp;
    //}

    [ContextMenu("Set 'From' to current value")]
    public override void SetStartToCurrentValue() { from = value; }

    [ContextMenu("Set 'To' to current value")]
    public override void SetEndToCurrentValue() { to = value; }

    [ContextMenu("Assume value of 'From'")]
    void SetCurrentValueToStart() { value = from; }

    [ContextMenu("Assume value of 'To'")]
    void SetCurrentValueToEnd() { value = to; }

}
